package com.kuang.servlet;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.net.URLDecoder;
import java.net.URLEncoder;
import java.util.Date;

/**
 * Author: Zhang Dongwei
 * Date: 2022/4/17 11:36
 */
// 中文数据怎么传递
public class CookieDemo03 extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        req.setCharacterEncoding("utf-8");
        resp.setCharacterEncoding("utf-8");

        PrintWriter out = resp.getWriter();
        // Cooike, 服务器端从客户端获取
        Cookie[] cookies = req.getCookies();// 这里返回数组，说明Cookie可能存在多个
        // 判断Cookie是否存在
        if(cookies!=null){
            // 如果存在怎么办
            out.write("你上一次访问时间是：");
            for (int i = 0; i < cookies.length; i++) {
                Cookie cookie = cookies[i];
                // 获取cookie的名字
                if(cookie.getName().equals("name")){
//                    System.out.println(cookie.getValue());
//                    URLDecoder.decode(cookie.getValue(),"UTF-8");
                    // 解码
                    out.write(URLDecoder.decode(cookie.getValue(),"UTF-8"));
                }
            }
        }else {
            out.print("这是您第一次访问本站");
        }

        // 编码
        Cookie cookie = new Cookie("name", URLEncoder.encode("张冬伟","utf-8"));
        resp.addCookie(cookie);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        doGet(req, resp);
    }
}
